<!DOCTYPE html>
<meta charset="utf-8">
<link rel="author" title="Joey Arhar" href="mailto:jarhar@chromium.org">
<link rel="help" href="https://github.com/WICG/display-locking">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>

<body>
  <div style="height: 2000px;">spacer</div>
  <script>
  async_test(t => {
    window.onload = t.step_func_done(() => {
      const foo = document.createElement('div');
      foo.textContent = 'foo';
      foo.id = 'foo';
      document.body.appendChild(foo);
      foo.addEventListener('beforematch', () => {
        // Move the matched element 2000px down the page.
        const spacer = document.createElement('div');
        spacer.style.height = '2000px';
        document.body.appendChild(spacer);
        foo.remove();
        document.body.appendChild(foo);
      });

      window.location.hash = '#foo';

      const offsetAfterMatch = window.pageYOffset;
      foo.scrollIntoView();
      assert_equals(offsetAfterMatch, window.pageYOffset, `The scroll after beforematch should be the same as scrolling directly to the element's final destination.`);
    });
  }, 'Verifies that when a beforematch event handler moves a matching element, we scroll to its final location.');
  </script>
</body>
